package com.niuquan.base_function;

import com.niuquan.base_data.Messages;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class NetTools {
	
	private static HttpClient httpclient = null;
	private static HttpPost httpPost = null;
	public static String send(String url,Messages message){
		
		httpclient = new DefaultHttpClient();
		httpPost = new HttpPost(url);
        HttpResponse response=null;
		String result = "fails";
		ArrayList parameters = new ArrayList();
		parameters.add(new BasicNameValuePair("json", message.tojstring()));
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");
			httpPost.setEntity(entity);
		    response = httpclient.execute(httpPost);
            if (response!=null){
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    result = EntityUtils.toString(response.getEntity());
                }else{
                    return result;
                }
            }else{
                return result;
            }
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return result;
	}

	public static String send(String url,Messages message,int page){

		httpclient = new DefaultHttpClient();
		httpPost = new HttpPost(url);
		httpPost.addHeader("Content-Type", "application/x-www-form-urluncoded");
		ArrayList parameters = new ArrayList();
		parameters.add(new BasicNameValuePair("json", message.tojstring()));
		parameters.add(new BasicNameValuePair("page", Integer.toString(page)));
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");
			httpPost.setEntity(entity);
			HttpResponse response = httpclient.execute(httpPost);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private String get_resstring(HttpResponse response)
	{
		StringBuffer buffer = new StringBuffer();

        try {
            BufferedReader reader = new BufferedReader(
            		new InputStreamReader(response.getEntity().getContent())
            );

            String temp = null;
            while ((temp = reader.readLine()) != null) {
                buffer.append(temp);
            }
            return buffer.toString();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		return null;
	}
}
